<?php defined('BASEPATH') OR exit('No direct script access allowed');

/**
 * Example
 *
 * This is an example of a few basic user interaction methods you could use
 * all done with a hardcoded array.
 *
 * @package		CodeIgniter
 * @subpackage	Rest Server
 * @category	Controller
 * @author		lcdung
 * @link		http://bungtay.com/hrm3/
*/

// This can be removed if you use __autoload() in config.php OR use Modular Extensions
require APPPATH.'/libraries/REST_Controller.php';

class Staff extends REST_Controller{
    
    function __construct(){
        parent::__construct();
        //$this->load->model('user','',TRUE);
        $this->load->model(array('staff_model','user_model'));

    }
    
    /**
     * 
     * Get one Staff by Id
     * @author lcdung
     * @link http://localhost/hrm/api/staff/staffById/id/$id/format/json
     * @param Integer $id
     * @return Json one Staff
     * 
     **/
    function byid_get(){
        
        $token = $this->get('token');
        $token_secret = $this->get('token_secret');

        $this->oauth($token);

        $id = $this->get('id');
        if(!$id){
        	$this->response(array('error' => 'Staff could not be found'), 404);
        }

        $oneStaff = $this->staff_model->getSingleStaff(array('idstaff'=>$id));
        
        if(!empty($oneStaff)){
           $this->response(array('result'=>$oneStaff)); 
        }else{
            $this->response(array('error' => 'Staff could not be found'), 404);
        }
    }
    
    /**
     * 
     * Get all List Staffs
     * @author lcdung
     * @link http://localhost/hrm/api/staff/staffs/format/json
     * @return Json List Staff
     * 
     **/
    function list_get(){
        $token = $this->get('token');
        $token_secret = $this->get('token_secret');

        $this->oauth($token);

        $limit = $this->get('limit');
        $offset = $this->get('offset');

        $staffs = $this->staff_model->getListStaff($limit,$offset);
        if($staffs){
            $this->response(array('result'=>$staffs));
        }else{
            $this->response(array('error' => 'No have any staff'), 404);
        }
    }
    
    /**
     * 
     * Update one Staff
     * @author lcdung
     * @link http://localhost/hrm/api/department/departmentEdit/id/
     * @return Json
     * 
     **/
    function add_post(){
        $params = json_decode(file_get_contents('php://input'), TRUE);

            $token = $this->get('token');

            

            $this->oauth($token);

            $param['codestaff'] = $params['codestaff'];

            $param['firstname'] = $params['firstname'];

            $param['lastname'] =  $params['lastname'];
            
            $param['iddepartment'] =  $params['iddepartment'];

            $param['gender'] =  $params['gender'];

            $param['mobile'] =  $params['mobile'];

            $param['address'] = $params['address'];

            $param["dateupdated"]   = time();

            $param["datecreated"]   = time();
            
            if($this->staff_model->addstaff($param)){
                $this->response(array('result'=>'true'), 200);    
            }else{
                $this->response(array('result'=>'false'), 200);    
            }
    }

    function update_post(){
        $params = json_decode(file_get_contents('php://input'), TRUE);

            $token = $this->get('token');

            

            $this->oauth($token);
                    
            $param['codestaff'] = $params['codestaff'];

            $param['firstname'] = $params['firstname'];

            $param['lastname'] =  $params['lastname'];
            
            $param['iddepartment'] =  $params['iddepartment'];

            $param['gender'] =  $params['gender'];

            $param['mobile'] =  $params['mobile'];

            $param['address'] = $params['address'];

            $param["dateupdated"]   = time();

            $condition = array('idstaff'=>$params['id']);

            if($this->staff_model->updateStaff($param,$condition)){
                $this->response(array('result'=>'true'), 200);    
            }else{
                $this->response(array('result'=>'false'), 200);    
            }
    }

    function delete_post(){
        $params = json_decode(file_get_contents('php://input'), TRUE);
            $token = $this->get('token');

            

            $this->oauth($token);
            if($this->staff_model->deleteStaff(array('idstaff'=>$params['id']))){
                $this->response(array('result'=>'true'), 200);    
            }else{
                $this->response(array('result'=>'false'), 200);    
            }
    }

    function oauth($token,$token_secret=null){
        //token=ab3cd9j4ks73hf7g&token_secret=xyz4992k83j47x0b
        $oauth = $this->user_model->auth_token($token);
        if($oauth){
            if(time()<$oauth['date_created']){
                    $cnttime = time()+10800;
                        
                    $data = array('date_created'=>$cnttime);
                    $cont = array('id' => $oauth['id']);
                    $xs = $this->user_model->updateKey($data,$cont);
                    //$this->response($xs, 200);
            }else{
                $this->response(array('error' => 'Wrong token'), 401);
            }
        }else{
            $this->response(array('error' => 'Wrong token'), 401);
        }
    }
        
}

?>